package com.example.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.dto.CommodityDTO;
import com.example.domain.Commodity;
import com.example.service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class MerchantCommodityController {

	private final CommodityService commodityService;

	@Autowired
	public MerchantCommodityController(CommodityService commodityService) {
		this.commodityService = commodityService;
	}

	/**
	 * 添加商品
	 *
	 * @return 返回的JSON字符串
	 */
	@PostMapping(value = "/merchant/commodity/add", produces = "application/json;charset=utf-8")
	public String addCommodity(String price, String type, @RequestParam("picture") MultipartFile picture, String text,
	                           String saleCount, String saleState, String historicalSaleCount, String peopleBuy,
	                           String appraiseScore, String tradingCount, String auditState, String userId) throws IOException {

		Map<String, Object> message = new HashMap<>(16);

		Commodity commodity = new Commodity();

		commodity.setPrice(Double.parseDouble(price));
		commodity.setType(type);
		commodity.setPicture(picture.getBytes());
		commodity.setText(text);
		commodity.setSaleCount(Integer.parseInt(saleCount));
		commodity.setProductState(Integer.parseInt(saleState));
		commodity.setHistoricalSaleCount(Integer.parseInt(historicalSaleCount));
		commodity.setPeopleBuy(Integer.parseInt(peopleBuy));
		commodity.setAppraiseScore(Double.parseDouble(appraiseScore));
		commodity.setTradingCount(Integer.parseInt(tradingCount));
		commodity.setAuditState(Integer.parseInt(auditState));
		commodity.setUserId(Integer.parseInt(userId));

		boolean b = commodityService.save(commodity);
		message.put("message", b ? 1 : -1);

		return JSON.toJSONString(message);
	}

	/**
	 * 分页查询当前商家用户的商品信息
	 *
	 * @param currentPage 当前页码
	 * @param pageSize    每页显示条数
	 * @return 返回的JSON字符串
	 */
	@PostMapping(value = "/merchant/commodity/definedMerchant/all", produces = "application/json;charset=utf-8")
	public String selectAllCommodityByLimitCurrentMerchant(Integer currentPage, Integer pageSize, Integer userId) {
		CommodityDTO commodityDTO = new CommodityDTO();
		commodityDTO.setUserId(userId).setCurrentPage(currentPage).setPageSize(pageSize);

		//根据条件查询
		return this.reAllByCondition(commodityDTO);
	}

	/**
	 * 根据商品名称分页查询当前商家用户的商品信息
	 *
	 * @param currentPage 当前页码
	 * @param pageSize    页面尺寸
	 * @param text        商品名称
	 * @param userId      当前商家用户id
	 * @return 返回的JSON字符串
	 */
	@PostMapping(value = "/merchant/commodity/definedMerchant/text", produces = "application/json;charset=utf-8")
	public String selectCommodityByTextCurrentMerchant(Integer currentPage, Integer pageSize, String text, Integer userId) {
		Map<String, Object> message = new HashMap<>(16);

		CommodityDTO commodityDTO = new CommodityDTO();
		commodityDTO.setUserId(userId).setText(text).setProductState(0).setAuditState(1).setCurrentPage(currentPage).setPageSize(pageSize);
		Page<Commodity> page = commodityService.getCommodityByMIdAndTextForM(commodityDTO);
		message.put("commodities", page.getRecords());
		message.put("total", page.getTotal());

		return JSON.toJSONString(message);
	}

	/**
	 * 修改指定商家用户的指定商品信息
	 *
	 * @return 返回的JSON字符串
	 */
	@PostMapping(value = "/merchant/commodity/definedMerchant/update", produces = "application/json;charset=utf-8")
	public String updateMerchantCommodity(Integer id, String price, String saleState, String appraiseScore,
	                                      String saleCount, Integer currentPage, Integer pageSize, Integer userId) {
		Commodity acceptCommodity = commodityService.getById(id);
		acceptCommodity.setProductState(Integer.parseInt(saleState));
		acceptCommodity.setPrice(Double.parseDouble(price));
		acceptCommodity.setAppraiseScore(Double.parseDouble(appraiseScore));
		acceptCommodity.setSaleCount(Integer.parseInt(saleCount));
		acceptCommodity.setUserId(userId);

		commodityService.updateById(acceptCommodity);
		CommodityDTO commodityDTO = new CommodityDTO();
		commodityDTO.setUserId(userId).setCurrentPage(currentPage).setPageSize(pageSize);

		return this.reAllByCondition(commodityDTO);
	}

	/**
	 * 查询指定商家用户所有待售的商品
	 *
	 * @param currentPage 当前页面
	 * @param pageSize    页面尺寸
	 * @param userId      商家用户id
	 * @return 返回的JSON字符串
	 */
	@PostMapping(value = "/merchant/commodity/definedMerchant/ready", produces = "application/json;charset=utf-8")
	public String selectCommodityReadyForBeingSold(Integer currentPage, Integer pageSize, Integer userId) {
		CommodityDTO commodityDTO = new CommodityDTO();
		commodityDTO.setUserId(userId).setCurrentPage(currentPage).setPageSize(pageSize)
				.setAuditState(1).setProductState(0);
		return this.reAllByCondition(commodityDTO);
	}

	/**
	 * 查询指定商家用户所有已下架的商品
	 *
	 * @param currentPage 当前页面
	 * @param pageSize    页面尺寸
	 * @param userId      商家用户id
	 * @return 返回的JSON字符串
	 */
	@PostMapping(value = "/merchant/commodity/definedMerchant/remove", produces = "application/json;charset=utf-8")
	public String selectCommodityRemove(Integer currentPage, Integer pageSize, Integer userId) {
		CommodityDTO commodityDTO = new CommodityDTO();
		commodityDTO.setUserId(userId).setCurrentPage(currentPage).setPageSize(pageSize)
				.setProductState(1);
		return this.reAllByCondition(commodityDTO);
	}

	/**
	 * 返回符合查询条件的全部数据
	 */
	private String reAllByCondition(CommodityDTO commodityDTO) {
		Map<String, Object> message = new HashMap<>(16);
		Page<Commodity> page = commodityService.getCommodityByMIdAndTextForM(commodityDTO);
		message.put("commodities", page.getRecords());
		message.put("total", page.getTotal());
		return JSON.toJSONString(message);
	}
}
